﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUtils;

namespace OAuth
{
    /// <summary>
    /// reads parameter string and turn them into encoded parameters
    /// eg
    /// Service Provider Response Parameters
    ///
    /// Response parameters are sent by the Service Provider to return Tokens and other information to the Consumer in the HTTP response body. The parameter names and values are first encoded as per Parameter Encoding (Parameter Encoding), and concatenated with the ‘&’ character (ASCII code 38) as defined in [RFC3986] (Berners-Lee, T., “Uniform Resource Identifiers (URI): Generic Syntax,” .) Section 2.1. For example:
    ///
    /// oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=xyz4992k83j47x0b

    /// </summary>
    public static class ParameterReader
    {
        public static IEnumerable<EncodedURLParameter> ParseParameters(string pParameterString)
        {
            return pParameterString
                .Split('&')
                .ForEach(str =>
                    {
                        if (str.IndexOf('=') != -1)
                        {
                            string[] parts = str.Split('=');
                            return new EncodedURLParameter()
                            {
                                Key = parts[0],
                                Value = parts[1]
                            };
                        }
                        else
                        {
                            return new EncodedURLParameter()
                            {
                                Key = str,
                                Value = string.Empty
                            };
                        }
                    });
        }
    }
}
